package com.zixi.beverage_order.service.seller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zixi.beverage_order.mapper.SellerMapper;
import com.zixi.beverage_order.pojo.Seller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SignUpInServiceImpl implements SignUpInService {
    @Autowired
    SellerMapper sellerMapper;

    @Override
    public Seller login(Seller seller) {
        Map<String, Object> map = new HashMap<>();
        map.put("phonenumber", seller.getPhonenumber());
        map.put("password", seller.getPassword());
        map.put("deleted", false);
        List<Seller> sellerLogins = sellerMapper.selectByMap(map);
        if (sellerLogins.isEmpty()) {
            seller.setDeleted(true);
            return seller;
        } else {//未来再写如果查出两个的异常
            return sellerLogins.get(0);
        }

    }

    @Override
    public Seller signup(Seller seller) {
        Map<String,Object> map =new HashMap<>();
        map.put("phonenumber",seller.getPhonenumber());
        if (sellerMapper.selectByMap(map).isEmpty()) {
            //不存在这个电话号码的话 准许插入
            seller.setDeleted(false);
            seller.setSignupdate(new Timestamp(System.currentTimeMillis()));
            sellerMapper.insert(seller);
            QueryWrapper<Seller> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("phonenumber",seller.getPhonenumber());
            queryWrapper.eq("password",seller.getPassword());
            return sellerMapper.selectOne(queryWrapper);
        }else{
            //存在这个电话号码
            seller.setDeleted(true);//标记为已经删除
            return seller;
        }
    }

    @Override
    public Seller shiroLogin(String phonenumber) {
        QueryWrapper<Seller> sellerQueryWrapper = new QueryWrapper<>();
        sellerQueryWrapper.eq("phonenumber",phonenumber);
        return sellerMapper.selectOne(sellerQueryWrapper);
    }

    @Override
    public void updateInfo(Seller seller) {
        sellerMapper.updateById(seller);
    }


}
